home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / src / morecore.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-19  |  3.1 KB  |  141 lines

  1. /*  $Id: morecore.c,v 1.2 1996/04/19 11:47:59 jan Exp $
  2.  
  3.     Part of XPCE
  4.     Designed and implemented by Anjo Anjewierden and Jan Wielemaker
  5.     E-mail: jan@swi.psy.uva.nl
  6.  
  7.     Copyright (C) 1995 University of Amsterdam. All rights reserved.
  8. */
  9.  
  10. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  11. This little module is included into   pl-save.c if FORCED_MALLOC_BASE is
  12. defined. It requires the GNU implementation   of malloc() and friends, a
  13. mmap() that handles MAP_ANON  (can  be  changed)   and  can  map  at the
  14. addresses normally used at the target machine.  I think this module will
  15. only work if you run Linux :-)  It   is  implemented to debug the win32s
  16. version, allocating from 0x80000000L.
  17. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  18.  
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <stdlib.h>
  22. #include <sys/mman.h>
  23. #include <fcntl.h>
  24. #include <string.h>
  25. extern int getpagesize(void);        /* no prototype */
  26.  
  27. #if 0                    /* include if not defined (old libc) */
  28. extern void * (*__morecore)(long size);
  29. #endif
  30.  
  31. static void *base;            /* base */
  32. static void *top;            /* external top */
  33. static void *atop;            /* allocated top */
  34. static int  pagsize;
  35.  
  36. #ifndef roundup
  37. #define roundup(p, n) (((p)+(n)-1 / (n)) * (n))
  38. #endif
  39.  
  40. #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
  41. #define MAP_ANON MAP_ANONYMOUS
  42. #endif
  43.  
  44. #define get_map_fd() (-1)
  45. #define STACK_MAP_TYPE MAP_ANON|MAP_PRIVATE|MAP_FIXED
  46.  
  47. void *
  48. mycore(long size)            /* similar to sbrk() */
  49. { void *mem = top;
  50.  
  51.   if ( top + size >= atop )
  52.   { void *ntop = (void *)roundup((ulong) top + size, pagsize);
  53.     if ( mmap(atop, ntop - atop,
  54.           PROT_READ|PROT_WRITE, STACK_MAP_TYPE, -1, 0L) != atop )
  55.     { fprintf(stderr, "mycore(): %s\n", strerror(errno));
  56.       return NULL;
  57.     }
  58.  
  59.     atop = ntop;
  60.   }
  61.  
  62.   top = top + size;
  63.   
  64.   return mem;
  65. }
  66.  
  67.  
  68. void
  69. start_memory(void * address)
  70. { pagsize    = getpagesize();
  71.  
  72.   base       = (void *)roundup((ulong) address, pagsize);
  73.   top         = base;
  74.   atop         = base;
  75.   __morecore = mycore;
  76. }
  77.  
  78. #ifdef TEST
  79.  
  80. #ifndef HAVE_STRTOUL
  81. ulong
  82. strtoul(const char *s, char **eptr, int base)
  83. { static unsigned char xmap[256];
  84.   static int xmap_initialised = 0;
  85.   unsigned long val;
  86.  
  87.   if ( !xmap_initialised )
  88.   { int n;
  89.  
  90.     xmap_initialised++;
  91.     for(n=0; n<256; n++)
  92.       xmap[n] = 0xff;
  93.     for(n='0'; n<='9'; n++)
  94.       xmap[n] = n - '0';
  95.     for(n='a'; n<='z'; n++)
  96.       xmap[n] = n - 'a' + 10;
  97.     for(n='A'; n<='Z'; n++)
  98.       xmap[n] = n - 'A' + 10;
  99.   }
  100.  
  101.   if ( s[0] == '0' && (s[1] == 'x' || s[1] == 'X') )
  102.     s += 2;
  103.  
  104.   for(val = 0; *s; s++)
  105.   { unsigned long dval = xmap[*s];
  106.  
  107.     if ( dval < (unsigned) base )
  108.       val = val * base + dval;
  109.     else
  110.     { if ( eptr )
  111.     *eptr = (char *)s;
  112.     }
  113.   }
  114.  
  115.   return val;
  116. }
  117. #endif
  118.  
  119.  
  120. main(int argc, char **argv)
  121. { static int testvals[] = { 1, 1000, 10000, 100000, -1 };
  122.   int *tv = testvals;
  123.   ulong base = 0x40000000L;
  124.  
  125.   if ( argc == 2 )
  126.     base = strtoul(argv[1], NULL, 16);
  127.  
  128.   start_memory((void *)base);
  129.  
  130.   for(; *tv != -1; tv++)
  131.   { char *rval = malloc(*tv);
  132.  
  133.     printf("malloc(%-6d) --> 0x%08x\n", *tv, rval);
  134.     memset(rval, 0xbf, *tv);
  135.   }
  136.  
  137.   return 0;
  138. }
  139.  
  140. #endif
  141.